package com.ramnight.sql2.component;

import java.util.HashMap;
import java.util.Set;

import com.ramnight.sql2.core.DB;
import com.ramnight.sql2.core.Sql2;
import com.ramnight.sql2.core.TABLE;
import com.ramnight.sql2.ext.ExMap;
import com.ramnight.sql2.util.SqlKit;

public class Model extends ExMap<Model>{
	private String table;
	
	Model(String table){
		map = new HashMap<>();
		this.table = table;
	}
	
	public Model set(String col, Object value){
		map.put(col, value);
		return this;
	}
	
	public Model setNull(String col){
		map.put(col, null);
		return this;
	}
	
	public Model setPk(Object pk){
		map.put(Sql2.getPk(), pk);
		return this;
	}
	
	public String table(){
		return table;
	}
	
	public Model table(String table){
		this.table = table;
		return this;
	}
	
	public boolean insert(){
		return DB.execute(
					SqlKit.generateInsertSql(this.table(),this.map()), 
					this.map().values().toArray());
	}
	
	public boolean update(Where where){
		return DB.execute(
					SqlKit.generateUpdateSql(this.table(),this.map(), where.sql()), 
					SqlKit.join(this.map().values(), where.param()));
	}
	
	public boolean updateBy(String col, Object value){
		return update(new Where(col, value));
	}
	
	public boolean updateByPk(Object pk){
		return updateBy(Sql2.getPk(), pk);
	}
	
	public boolean updateByPk(){
		return this.updateByPk(this.getPk());
	}
	
	public boolean replace(){
		return DB.execute(
					SqlKit.generateReplaceSql(this.table(),this.map()), 
					this.map().values().toArray());
	}
	
	public boolean delete(Where where){
		return DB.execute(
					SqlKit.generateDeleteSql(this.table(), where.sql()), 
					where.param().toArray());
	}
	
	public boolean deleteBy(String col, Object value){
		return delete(new Where(col, value));
	}
	
	public boolean deleteByPk(Object pk){
		return deleteBy(Sql2.getPk(), pk);
	}
	
	public boolean deleteByPk(){
		return deleteBy(Sql2.getPk(), this.getPk());
	}
	
	public boolean increase(String col, Object num){
		Where where = new Where(Sql2.getPk(), this.getPk());
		return increase(col, num, where);
	}
	
	public boolean increase(String col, Object num, Where where){
		return DB.execute(
				SqlKit.generateIncreaseSql(true, table(), col, where.sql()), 
				SqlKit.join(num, where.param()));
	}
	
	public boolean decrease(String col, Object num){
		Where where = new Where(Sql2.getPk(), this.getPk());
		return decrease(col, num, where);
	}
	
	public boolean decrease(String col, Object num, Where where){
		return DB.execute(
				SqlKit.generateIncreaseSql(false, table(), col, where.sql()), 
				SqlKit.join(num, where.param()));
	}
	
	public Model retainCols(){
		Set<String> cols = cols();
		this.retain(cols.toArray(new String[cols.size()]));
		return this;
	}
	
	public Set<String> cols(){
		return TABLE.getCols(table);
	}
	
	public Model reload(){
		this.map(new Query(table, this.getPk()).find().map());
		return this;
	}
	
	public Model reloadBy(String col){
		this.map(new Query(table).where(col, this.get(col)).find().map());
		return this;
	}
	
	public Model reloadBy(String col, Object value){
		this.map(new Query(table).where(col, value).find().map());
		return this;
	}
}
